﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AllDemo
{
    public partial class 矩形绕任意点旋转 : Form
    {
        private int enable = 0;

        public 矩形绕任意点旋转()
        {
            InitializeComponent();
            MouseDown += new MouseEventHandler(OnMouseDown);
            Text = "右键确定原点";
            this.DoubleBuffered = true;    //开启二次缓存
            
        }

        PointF tempPoint1 = new PointF(300, 300);    //需要旋转的点
        PointF tempPoint2 = new PointF(500, 300);    //需要旋转的点
        PointF tempPoint3 = new PointF(300, 400);    //需要旋转的点
        PointF tempPoint4 = new PointF(500, 400);    //需要旋转的点
        Pen myPen = new Pen(Color.Orange, 1f);
        private float ox = 350;    //原点
        private float oy = 350;    //原点

        private double angle = 1.0;   //旋转角度


        void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ox = e.X;
                oy = e.Y;
            }
        }
      

        private void Form4_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();

            PointF pointO = new PointF(ox, oy);
            g.DrawLine(Pens.Red, ox - 5, oy, ox + 5, oy);
            g.DrawLine(Pens.Red, ox, oy - 5, ox, oy + 5);  //绘制原点
            g.FillEllipse(Brushes.Red, tempPoint1.X, tempPoint1.Y, 5, 5);//绘制第一个点
            g.FillEllipse(Brushes.Red, tempPoint2.X, tempPoint2.Y, 5, 5);//绘制第二个点
            g.FillEllipse(Brushes.Red, tempPoint3.X, tempPoint3.Y, 5, 5);//绘制第三个点
            g.FillEllipse(Brushes.Red, tempPoint4.X, tempPoint4.Y, 5, 5);//绘制第四个点
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            if (enable > 0)
            {
                try
                {
                    angle = Convert.ToDouble(txt_angle.Text);
                    //angle= rd.Next(1,10);
                    angle *= Math.PI / 180;
                }
                catch (Exception)
                {


                }

                Graphics g = e.Graphics;

                PointF pointO = new PointF(ox, oy);
                g.DrawLine(Pens.Red, ox - 5, oy, ox + 5, oy);
                g.DrawLine(Pens.Red, ox, oy - 5, ox, oy + 5);  //绘制原点


                PointF newPoint1 = Transition(tempPoint1.X - ox, tempPoint1.Y - oy, angle);
                tempPoint1 = newPoint1;


                PointF newPoint2 = Transition(tempPoint2.X - ox, tempPoint2.Y - oy, angle);
                tempPoint2 = newPoint2;

                PointF newPoint3 = Transition(tempPoint3.X - ox, tempPoint3.Y - oy, angle);
                tempPoint3 = newPoint3;

                PointF newPoint4 = Transition(tempPoint4.X - ox, tempPoint4.Y - oy, angle);
                tempPoint4 = newPoint4;

                g.DrawLine(myPen, tempPoint1.X, tempPoint1.Y, tempPoint2.X, tempPoint2.Y);
                g.DrawLine(myPen, tempPoint2.X, tempPoint2.Y, tempPoint4.X, tempPoint4.Y);
                g.DrawLine(myPen, tempPoint4.X, tempPoint4.Y, tempPoint3.X, tempPoint3.Y);
                g.DrawLine(myPen, tempPoint3.X, tempPoint3.Y, tempPoint1.X, tempPoint1.Y);
            }


        }
        private PointF Transition(float _x, double _y, double _angle)
        {
            double newX = _x * Math.Cos(_angle) - _y * Math.Sin(_angle) + ox;
            double newY = _x * Math.Sin(_angle) + _y * Math.Cos(_angle) + oy;
            return new PointF((float)newX, (float)newY);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }



       
       

        private void btn_begin_Click(object sender, EventArgs e)
        {
            enable = 1;
            Invalidate();
        }
    }
}
